package com.google.code.sip.ucenter.ws.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.code.sip.ucenter.model.Setting;
import com.google.code.sip.ucenter.service.SettingManager;
import com.google.code.sip.ucenter.ws.WSResponse;
import com.google.code.sip.ucenter.ws.WSResponseStatus;
import com.google.code.sip.ucenter.ws.dto.SettingDto;
import com.sun.jersey.spi.resource.Singleton;

@Component
@Path("/test")
@Singleton
@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public class TestResource extends AbstractResource {
	@Autowired
	private SettingManager settingManager;

	@Path("test")
	@GET
	public WSResponse<SettingDto> test() {
		List<Setting> settings = settingManager
				.getAllSettings(Setting.GROUP_CREDIT);
		if (settings == null || settings.isEmpty()) {
			return new WSResponse<SettingDto>(new WSResponseStatus(-1,
					"无积分系统参数。"));
		}
		List<SettingDto> dtos = new ArrayList<SettingDto>(settings.size());
		for (Setting setting : settings) {
			SettingDto dto = new SettingDto();
			BeanUtils.copyProperties(setting, dto);
			dtos.add(dto);
		}
		return new WSResponse<SettingDto>(dtos);
	}
}
